package com.small.admin.controller;

import com.small.admin.entity.Admin;
import com.small.admin.config.constant.MsgAdmin;
import com.small.admin.dto.Login;
import com.small.common.manager.redis.RedisClient;
import com.small.common.msg.Msg;
import com.small.common.msg.MsgCommon;
import com.small.common.util.encrypt.MD5;
import com.small.push.bo.OperatorEmailMsg;
import com.small.push.bo.PushMsg;
import com.small.push.producer.MsgProducer;

import org.springframework.beans.factory.annotation.Autowired;
//import com.small.common.util.val.captcha.CaptchaGenerate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017/10/17 16:46
 * 描述: 登录
 */
@RestController
public class LoginController extends BaseLoginController {

    private static final long MILLISECOND = 3 * 60 * 60 * 1000;//3小时

//    @Resource
//    private CaptchaGenerate captchaGenerate;
    
    @Autowired
    private RedisClient redisClient;
    
    @Autowired
    private MsgProducer msgProducer;

    @GetMapping("test/status")
    public String getTest(String name){
//        redisClient.hset(name, "gs", "liangcaowei");
//        System.out.println(redisClient.hget(name, "gs"));
//        return redisClient.hget(name, "gs");
        
        PushMsg msg = new OperatorEmailMsg()
                .setResultId(4L).setNoticeId(5L)
                .setSubject("subject")
                .setCustomerName(name)
                .setContact("15821870051")
                .setFromUser(3L);
                boolean result = msgProducer.send("redis-topic:topic2", msg);
                return "ok:"+result;
//        String result = redisClient.brpop("redis-topic:topic1");    
//        return "ok:"+result;
                
    }
    
    @PostMapping("login")
    public Msg login(HttpServletRequest request, @Valid @RequestBody Login login) throws Exception {
        //账号不存在或者账号密码错误
        Msg result = MsgCommon.TIP_10008;
//        if (captchaGenerate.check(request, login.getCode())) {
            Admin admin = adminService.selectByAccount(login.getAccount());
            if (admin != null) {
                long currentTime = System.currentTimeMillis();
                //账号已被禁用
                if (admin.getState() == 0) {
                    result = MsgAdmin.TIP_ADMIN_DISABLE;
                } else if (admin.getState() == 2 && (admin.getSealDate() == null || admin.getSealDate() + MILLISECOND >= currentTime)) {
                    //封号3小时
                    result = MsgAdmin.TIP_LOGIN_SEAL_ACCOUNT;
                } else if (!admin.getPassword().equals(MD5.encrypt32(login.getPassword()))) {
                    //确保3小时以后可以再允许密码错误5次
                    Integer loginError = admin.getLoginError();
                    if (loginError == null) {
                        loginError = 0;
                    }
                    Admin updateAdmin = new Admin();
                    updateAdmin.setId(admin.getId());
                    updateAdmin.setUid(admin.getId());
                    updateAdmin.setUtime(currentTime);
                    updateAdmin.setLoginError(++loginError);
                    if (loginError >= 5) {
                        updateAdmin.setState(2);
                        updateAdmin.setLoginError(0);
                        updateAdmin.setSealDate(currentTime);
                    }
                    adminService.updateSelectiveById(updateAdmin);
                } else {
                    //登录成功
                    result = storageSession(admin);
                }
            } else {
                result = MsgAdmin.TIP_LOGIN_ACCOUNT_PWD;
            }
//        }
        return result;
    }

    @GetMapping("isLogin")
    @ResponseBody
    public Msg isLogin() {
        return MsgAdmin.SUCCESS;
    }

}
